package com.orangelife.mobile.goodsrent.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.common.util.OrangePhoneUtil;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.complaints.biz.Complaints;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.goodsrent.biz.GoodsRentBiz;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by YangWei
 * on 2015/11/12.
 */
public class GoodsRentAddJudgementActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.llRightClick)
    LinearLayout llRightClick;
    @Bind(R.id.v1)
    View v1;
    @Bind(R.id.rbJudge)
    RatingBar rbJudge;
    @Bind(R.id.btnSubmit)
    Button btnSubmit;
    int status;
    String orderID;
    String scorerID;
    Map<String, Object> map;
    int flag = 2;

    private Dialog submitDialog;

    public static boolean ISFRESH = false;

    Complaints complaints = Complaints.getInstance();
    /** 请求地址 */
    String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodsrent_add_judgement);
        ButterKnife.bind(this);
        CurryApplication.getInstance().addActivity(this);
        initView();
        findView();
    }

    /**
     * 初始化
     */
    private void initView() {
        map = new HashMap<>();
        Intent data = getIntent();
        String statusStr = data.getStringExtra("status");
        orderID = data.getStringExtra("orderID");
        scorerID = data.getStringExtra("scorerID");
        if (!StringUtil.isBlank(statusStr)) {
            status = Integer.parseInt(statusStr);
        }
        ivTitleRight.setVisibility(View.VISIBLE);
        ivTitleRight.setBackgroundResource(R.drawable.icon_phone);
        ISFRESH = false;

    }

    /**
     * 赋值并判断
     */
    private void findView() {
        if (status == 3) {
            tvTitle.setText("添加评价");
            ivTitleRight.setOnClickListener(listener);
            btnSubmit.setOnClickListener(listener);
        } else if (status == 4) {
            tvTitle.setText("评价详情");
            btnSubmit.setVisibility(View.GONE);
            ivTitleRight.setOnClickListener(listener);
            rbJudge.setIsIndicator(true);
            getEvaluate();

        }
    }

    /**
     * 设置监听器
     */
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_title_right:
                    OrangePhoneUtil.callCommProperty(GoodsRentAddJudgementActivity.this);
                    break;
                case R.id.btnSubmit:
                    url = Constant.URL_GOODRENT_ORDER_ADD_JUDGE;
                    if (rbJudge.getRating() != 0) {
                        int score = (int) rbJudge.getRating();
                        submitDialog = DialogHelper.getInstance().createLoadingDialog(
                                GoodsRentAddJudgementActivity.this,
                                Constant.SUBMIT_LOADING);
                        HashMap<String, Object> mapAdd = new HashMap<>();
                        mapAdd.put("orderID", orderID);
                        mapAdd.put("score", score + "");
//                        mapAdd.put("scorerID", scorerID);
                        GoodsRentBiz.getInstance().requesPost(null, mapAdd, new VolleyHttpUtil.ResponseCallBack() {
                            @Override
                            public void handleResponse(JSONObject response, int errCode) {
                                DialogHelper.closeDialog(submitDialog);
                                if (OrangeErrorHandler.getInstance().isSuccess(errCode)) {
                                    ToastHelper.getInstance()._toast(getString(R.string.submit_success));
                                    ISFRESH = true;
                                    finish();
                                }
                            }
                        }, url);

                    } else {
                        ToastHelper.getInstance()._toast("请先评价后提交");
                    }
                    break;
            }
        }
    };

    /**
     * 获取评价详情
     */
    private void getEvaluate() {
        url = Constant.URL_GOODRENT_ORDER_JUDGE_DETAIL;
        HashMap<String, String> map = new HashMap<>();
        map.put("{orderID}", orderID);
        GoodsRentBiz.getInstance().requestGet(map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                DialogHelper.closeDialog(dialog);
                if (OrangeErrorHandler.getInstance().isSuccess(response)) {
                    Map<String, Object> evaluateMap = new HashMap<String, Object>();
                    evaluateMap = GoodsRentBiz.getInstance().handleOrderDetail(response, errCode);
                    if (!StringUtil.isContainBlank(String.valueOf(evaluateMap.get("score")))) {
                        rbJudge.setRating(Integer.parseInt(String.valueOf(evaluateMap.get("score"))));
                    }
                }
            }
        }, url);
    }
}
